﻿@page "/GridSelectionMultiple"
@using Demo.Blazor.Model
@using Demo.Blazor.Services
@inject FlatProductService FlatProductService
   
 <div class="card demo-card-wide">
        <div class="card-header">@nameof(Name)=<b>@(Name)</b>, @nameof(Email)=<b>@(Email)</b>, @nameof(Birthday)=<b>@(Birthday)</b>, @nameof(Worked)=<b>@(Worked)</b>, @nameof(AtVacation)=<b>@(AtVacation)</b></div>
        <div class="card-body">
            <DxFormLayout>
                <DxFormLayoutItem Caption="Contact Name:" ColSpanMd="6">
                    <Template>
                        <DxTextBox @bind-Text="@Name"></DxTextBox>
                    </Template>
                </DxFormLayoutItem>

                <DxFormLayoutItem Caption="Email:" ColSpanMd="6">
                    <Template>
                        <DxTextBox @bind-Text="@Email"></DxTextBox>
                    </Template>
                </DxFormLayoutItem>

                <DxFormLayoutItem Caption="Birthday:" ColSpanMd="6">
                    <Template>
                        <DxDateEdit @bind-Date="@Birthday"></DxDateEdit>
                    </Template>
                </DxFormLayoutItem>

                <DxFormLayoutItem Caption="Worked year(s):" ColSpanMd="6">
                    <Template>
                        <DxSpinEdit @bind-Value="@Worked"></DxSpinEdit>
                    </Template>
                </DxFormLayoutItem>

                <DxFormLayoutItem Caption="At Vacation:">
                    <Template>
                        <DxCheckBox @bind-Checked="@AtVacation"></DxCheckBox>
                    </Template>
                </DxFormLayoutItem>
            </DxFormLayout>
        </div>
    </div>

@if (DataSource == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <div class="card demo-card-wide border-bottom-0" style="border-bottom-left-radius: 0; border-bottom-right-radius: 0">
        <div class="card-header border-bottom-0">The total count of selected rows: <b>@SelectedCount</b> (stored selected rows: <b>@StoredSelectedCount</b>, stored unselected rows: <b>@StoredUnselectedCount</b>)</div>
    </div>

    <DxDataGrid Data="@DataSource" @ref="@grid"
                CssClass="datagrid-border-radius-top-0"
                KeyFieldName="Id"
                SelectionMode="DataGridSelectionMode.OptimizedMultipleSelection"
                SelectAllMode="DataGridSelectAllMode.AllPages"
                OptimizedMultipleSelectionChanged="OnSelectionChanged"
                ShowFilterRow="false">
        <DxDataGridSelectionColumn Width="50px"></DxDataGridSelectionColumn>
        <DxDataGridColumn Field="@nameof(ProductFlat.ProductName)" Caption="Product Name"></DxDataGridColumn>
        <DxDataGridColumn Field="@nameof(ProductFlat.Category)"></DxDataGridColumn>
        <DxDataGridColumn Field="@nameof(ProductFlat.Subcategory)"></DxDataGridColumn>
    </DxDataGrid>

    <div class="code-snippet card demo-card-wide">

    </div>
}

@code {

    string name = "Nancy Davolio";
    string Name { get => name; set { name = value; OnChanged(); } }

    string email = "NancyDavolio@sample.com";
    string Email { get => email; set { email = value; OnChanged(); } }

    DateTime birthday = DateTime.Now.AddYears(-20);
    DateTime Birthday { get => birthday; set { birthday = value; OnChanged(); } }

    int worked = 3;
    int Worked { get => worked; set { worked = value; OnChanged(); } }

    bool atVacation = true;
    bool AtVacation { get => atVacation; set { atVacation = value; OnChanged(); } }

    void OnChanged() { InvokeAsync(StateHasChanged); }


    DxDataGrid<ProductFlat> grid;

    IQueryable<ProductFlat> DataSource;

    int SelectedCount { get; set; }

    int StoredSelectedCount { get; set; }

    int StoredUnselectedCount { get; set; }

    protected override async Task OnInitializedAsync()
    {
        DataSource = await FlatProductService.Load();
    }

    protected override Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            var products = DataSource.Take(10).AsEnumerable().Where((p, i) => i % 2 != 0);
            foreach (var product in products)
                grid.SetDataRowSelected(product, true);
        }
        return base.OnAfterRenderAsync(firstRender);
    }

    protected async Task OnSelectionChanged(DataGridSelection<ProductFlat> selection)
    {
        StoredSelectedCount = selection.SelectedKeysStored.Count();
        StoredUnselectedCount = selection.UnselectedKeysStored.Count();
        var selectedKeys = await selection.SelectedKeys;
        SelectedCount = selectedKeys.Count();
        await InvokeAsync(StateHasChanged);
    }
}